package com.eol.common.rabbitMq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: liulinhui
 * Date: 2020/1/9
 * Time: 5:56 PM
 * Description: OrderFinishRabbitMqConfig
 */
@Configuration
public class OrderFinishRabbitMqConfig {

    /**
     * 声明死信队列、死信交换机、绑定队列到死信交换机
     * 建议使用FanoutExchange广播式交换机
     */
    @Bean
    public Queue orderFinishDeadLettersQueue() {
        return new Queue(RabbitMqConstants.BBQ_ORDER_FINISH_DEAD_LETTERS_QUEUE_NAME);
    }

    @Bean
    public FanoutExchange orderFinishDeadLettersExchange() {
        return new FanoutExchange(RabbitMqConstants.BBQ_ORDER_FINISH_DEAD_LETTERS_EXCHANGE_NAME);
    }

    @Bean
    public Binding orderFinishDeadLettersBinding() {
        return BindingBuilder.bind(orderFinishDeadLettersQueue()).to(orderFinishDeadLettersExchange());
    }

    /**
     * 声明普通队列，并指定相应的备份交换机、死信交换机
     */
    @Bean
    public Queue orderFinishQueue() {
        Map<String, Object> arguments = new HashMap<>(10);
        //指定死信发送的Exchange
        arguments.put("x-dead-letter-exchange", RabbitMqConstants.BBQ_ORDER_FINISH_DEAD_LETTERS_EXCHANGE_NAME);
        return new Queue(RabbitMqConstants.BBQ_ORDER_FINISH_QUEUE_NAME, true, false, false, arguments);
    }

    @Bean
    public Exchange orderFinishExchange() {
        return new DirectExchange(RabbitMqConstants.BBQ_ORDER_FINISH_EXCHANGE_NAME, true, false, null);
    }

    @Bean
    public Binding orderFinishBinding() {
        return BindingBuilder.bind(orderFinishQueue()).to(orderFinishExchange()).with(RabbitMqConstants.BBQ_ORDER_FINISH_ROUTING_KEY).noargs();
    }

}
